<template>
    <div class="login-container" style="text-align: center;">
      <h2>登录</h2>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="账号" prop="username">
      <el-input type="username" v-model="ruleForm.username" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="login">登录</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
    </div>
  </template>
  
  <script>

     export default {
      
      data() {
        var validateAccount = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入账号'));
          }
            callback();
          }
        var validatePass = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入密码'));
          }
            callback();
          }
        return {
          ruleForm: {
            username:'',
            password:''
          },
          rules: {
            password: [
              { validator: validatePass, trigger: 'blur' }
            ],
            username: [
              { validator: validateAccount, trigger: 'blur' }
            ]
          }
        };
      },
      methods: {
        login() {
              const loading=this.$loading({
                lock:true,
                text:'登陆中',
                spinner:'el-icon-loading',
                background:'rgba(0,0,0,0.2)'
              });
              this.axios.post("/api/login",this.ruleForm).then(rep=>{
                if(rep.data!=""){
                loading.close();
                sessionStorage.setItem("name",this.ruleForm.username)
                  this.$router.push("/home")
                }else{
                  this.$message({
                    message:'登陆失败，账号或密码错误!',
                    type:'error',
                    duration:1500
                  });
                  loading.close();
                }
              })
            },
            
        resetForm(formName) {
          this.$refs[formName].resetFields();
        }

      }
     }
  </script>
  
  <style>
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;  
  }
  </style>
  